﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Common.Data.Converters="clr-namespace:Imagin.Common.Data.Converters;assembly=Imagin.Common"
    xmlns:Controls.Common="clr-namespace:Imagin.Controls.Common;assembly=Imagin.Controls.Common">
    <Common.Data.Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

    <ControlTemplate x:Key="Template.ComboBox" TargetType="{x:Type ComboBox}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="15"/>
            </Grid.ColumnDefinitions>
            <ToggleButton 
                Grid.ColumnSpan="2" 
                x:Name="PART_ToggleButton" 
                Foreground="{TemplateBinding Foreground}"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Focusable="false" 
                IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
                ClickMode="Press">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="20" />
                            </Grid.ColumnDefinitions>
                            <Border 
                                x:Name="Border" 
                                Grid.ColumnSpan="2" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"/>
                            <Path 
                                Grid.Column="1" 
                                x:Name="PART_Arrow" 
                                Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                                Fill="{TemplateBinding Foreground}" 
                                HorizontalAlignment="Center"  
                                RenderTransformOrigin="0.5, 0.5"
                                VerticalAlignment="Center"
                                Visibility="{Binding IsReadOnly, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverted, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}">
                                <Path.RenderTransform>
                                    <RotateTransform/>
                                </Path.RenderTransform>
                            </Path>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="PART_Arrow" Storyboard.TargetProperty="(Path.RenderTransform).(RotateTransform.Angle)" To="90" Duration="0:0:0.4"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                                <Trigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="PART_Arrow" Storyboard.TargetProperty="(Path.RenderTransform).(RotateTransform.Angle)" To="0" Duration="0:0:0.4"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.ExitActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </ToggleButton.Template>
            </ToggleButton>
            <ContentPresenter 
                x:Name="ContentSite" 
                IsHitTestVisible="False" 
                Content="{TemplateBinding SelectionBoxItem}" 
                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                HorizontalAlignment="Left"
                Margin="{TemplateBinding Padding}"
                VerticalAlignment="Center">
            </ContentPresenter>
            <Controls.Common:TextBoxExt 
                x:Name="PART_TextBox" 
                Focusable="True" 
                Background="Transparent" 
                BorderThickness="0"
                Foreground="{TemplateBinding Foreground}" 
                Margin="8,0"  
                Padding="0"
                Visibility="Collapsed" 
                Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
                IsReadOnly="{TemplateBinding IsReadOnly}"/>
            <Popup 
                x:Name="Popup" 
                AllowsTransparency="True"
                Focusable="False" 
                IsOpen="{TemplateBinding IsDropDownOpen}" 
                Placement="Bottom" 
                PopupAnimation="Slide">
                <Grid 
                    x:Name="DropDown" 
                    SnapsToDevicePixels="True"
                    MinWidth="{TemplateBinding ActualWidth}" 
                    MaxHeight="{TemplateBinding MaxDropDownHeight}">
                    <Border 
                        x:Name="DropDownBorder" 
                        Background="{DynamicResource Brush.ComboBox.DropDown.Background}"
                        BorderBrush="{DynamicResource Brush.ComboBox.DropDown.Border}" 
                        BorderThickness="1"/>
                    <ScrollViewer Margin="1,2,1,1" SnapsToDevicePixels="True">
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                    </ScrollViewer>
                </Grid>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="HasItems" Value="False">
                <Setter TargetName="DropDownBorder" Property="MinHeight" Value="125" />
            </Trigger>
            <Trigger Property="IsEditable" Value="true">
                <Setter TargetName="PART_TextBox" Property="Visibility" Value="Visible" />
                <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="Style.ComboBox" TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="{DynamicResource Brush.ComboBox.Background}" />
        <Setter Property="BorderBrush" Value="{DynamicResource Brush.ComboBox.Border}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Foreground" Value="{DynamicResource Brush.ComboBox.Foreground}" />
        <Setter Property="Padding" Value="7,5" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="Width" Value="Auto" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="{DynamicResource Brush.ComboBox.MouseOver.Background}" />
                <Setter Property="BorderBrush" Value="{DynamicResource Brush.ComboBox.MouseOver.Border}" />
                <Setter Property="Foreground" Value="{DynamicResource Brush.ComboBox.MouseOver.Foreground}"/>
            </Trigger>
            <Trigger Property="IsGrouping" Value="True">
                <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
            </Trigger>
            <Trigger Property="IsEditable" Value="True">
                <Setter Property="IsTabStop" Value="False" />
            </Trigger>
            <Trigger Property="IsDropDownOpen" Value="True">
                <Setter Property="Background" Value="{DynamicResource Brush.ComboBox.Checked.Background}" />
                <Setter Property="BorderBrush" Value="{DynamicResource Brush.ComboBox.Checked.Border}" />
                <Setter Property="Foreground" Value="{DynamicResource Brush.ComboBox.Checked.Foreground}"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.6" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource Style.ComboBox}">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Template" Value="{StaticResource Template.ComboBox}"/>
    </Style>

    <Style x:Key="{x:Type Controls.Common:AdvancedComboBox}" TargetType="{x:Type Controls.Common:AdvancedComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"/>

    <Style x:Key="{x:Type Controls.Common:FontFamilyBox}" TargetType="{x:Type Controls.Common:FontFamilyBox}" BasedOn="{StaticResource {x:Type ComboBox}}"/>

    <Style x:Key="{x:Type Controls.Common:FontSizeBox}" TargetType="{x:Type Controls.Common:FontSizeBox}" BasedOn="{StaticResource {x:Type ComboBox}}"/>

    <Style x:Key="{x:Type Controls.Common:TreeViewComboBox}" TargetType="{x:Type Controls.Common:TreeViewComboBox}" BasedOn="{StaticResource Style.ComboBox}"/>

    <Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Foreground" Value="{DynamicResource Brush.ComboBoxItem.Foreground}" />
        <Setter Property="Padding" Value="6,5" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border 
                        x:Name="Border" 
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}" 
                        SnapsToDevicePixels="True">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter VerticalAlignment="Center"/>
                            <Controls.Common:MaskedImage
                                Grid.Column="1"
                                x:Name="PART_Checkmark"
                                ImageColor="{TemplateBinding Foreground}"
                                Margin="5,0,0,0"
                                Source="pack://application:,,,/Imagin.Styles;component/Images/CheckmarkRound.png"
                                VerticalAlignment="Center"
                                Visibility="Collapsed"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" Value="0.6" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="{DynamicResource Brush.ComboBoxItem.Selected.Background}" />
                            <Setter Property="Foreground" Value="{DynamicResource Brush.ComboBoxItem.Selected.Foreground}" />
                            <Setter TargetName="PART_Checkmark" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"/>
                                <Condition Property="IsSelected" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="{DynamicResource Brush.ComboBoxItem.MouseOver.Background}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>